var express = require("express"),
		getColumn = require("./public/getColumn"),
		article = require("../../models/adminSite/article"),
		bannerS = require("../../models/adminSite/banner"),
		articleFun = new article(),
		router = express.Router(),
		hotArticle = "",
		bannerList = "",
		hot = function (req , res , next){
			articleFun.getArticle({Popular : 1} , function(error , data){
				if(error){
					hotArticle = null;
				};
				(hotArticle = data) && next();
			});
		},
		banner = function(req , res , next){
			bannerS.getBanner({} , function(error , data){
				if(error){
					bannerList = null;
				};
				(bannerList = data) && next();
			});
		};
//首页
router.get("/" , hot , banner , function(req , res){
	articleFun.getArticle(req.query , function(error , article){
		if(error){
			return res.end("<h2>操作失败</h2>");
		};
		//获取文章栏目
		getColumn(function(column){
			columnData = column;
			res.render("webSite/index" , {
				article : article,
				hotArticle : hotArticle,
				column : column,
				banner : bannerList,
				user : req.session.user,
				error : ""
			});
		});
	});
});
//加载更多
router.post("/getArticle" , function(req , res){
	articleFun.getArticle(req.body , function(error , article){
		if(error){
			return res.json({
				state : "error",
				message : "操作失败"
			});
		};
		return res.json({
			state : "ok",
			message : "",
			data : article
		});
	});
})
//接口
module.exports = router;
